﻿using System;
using System.Collections.Generic;

namespace InDepth2Demo.Chapter6
{
    public class YieldDemo
    {
        public IEnumerable<int> GetEvenNumbers(int max)
        {
            for (int i = 0; i <= max; i++)
            {
                if (i % 2 == 0)
                {
                    yield return i; // 返回偶数，并在此处暂停执行
                }
            }
        }

        public static void Call()
        {
            YieldDemo yield = new YieldDemo();
            foreach (int evenNumber in yield.GetEvenNumbers(10))
            {
                Console.WriteLine(evenNumber); // 输出0, 2, 4, 6, 8, 10
            }
        }
    }
}
